﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 创建SQLServer类型的数据服务
    /// </summary>
    [CPPEI.WebApi.AuthPostAction("SQL", "CreateDataService", "1.0")]
    public class CreateSQLDataServiceAction : BaseAction<CreateSQLDataServiceParams, CreateSQLDataServiceResult>
    {
        protected override ActionResult<CreateSQLDataServiceResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            CreateSQLDataServiceParams opParams)
        {
            Guid categoryId = new Guid("{D862B06A-6045-4162-AC5C-9F144039711F}");
            Guid typeId = new Guid("{FBC88932-8500-4D98-B795-FF84AEB002C3}");

            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult<ENT.DtSer> serResult = dtSerService.CreateDtSer(context.UserId, opParams.FolderId, opParams.Name, 
                string.Format("{0},{1}", opParams.Server, opParams.Database),
                opParams.NeedAuth,
                opParams.AuthUser,
                opParams.AuthPassword,
                categoryId,
                typeId,
                opParams.Description);
            if(serResult.IsSuccess)
            {
                CreateSQLDataServiceResult result = new CreateSQLDataServiceResult();
                result.Id = serResult.Data.Id;
                return new ActionResult<CreateSQLDataServiceResult>(result);
            }
            else
            {
                return new ActionResult<CreateSQLDataServiceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class CreateSQLDataServiceParams
    {
        [Parameter]
        public string Name { get; set; }

        [Parameter]
        public bool NeedAuth { get; set; }

        [Parameter]
        public string AuthUser { get; set; }

        [Parameter]
        public string AuthPassword { get; set; }

        [Parameter]
        public string Server { get; set; }
        [Parameter]
        public string Database { get; set; }

        [Parameter]
        public string Description { get; set; }

        [Parameter]
        public Guid FolderId { get; set; }
    }

    public class CreateSQLDataServiceResult
    {
        public Guid Id { get; set; }
    }
}
